/**
 * @file 外部FLASH 的 SPI驱动
 * @author
 * @date
 * @brief
 */
#ifndef FLASH_SPI_DRV_H_
#define FLASH_SPI_DRV_H_
#include "stdint.h"

#define sFLASH_DUMMY_BYTE 0x55

/**
 * @ SPI NOR CMD
 */

#define sFLASH_CMD_NOR_WRITE 0x02 /*!< Write to Memory instruction */
#define sFLASH_CMD_NOR_WRSR 0x01  /*!< Write Status Register instruction */
#define sFLASH_CMD_NOR_WREN 0x06  /*!< Write enable instruction */
#define sFLASH_CMD_NOR_WDIS 0x04  /*!< Write disable instruction */
#define sFLASH_CMD_NOR_READ 0x03  /*!< Read from Memory instruction */
#define sFLASH_CMD_NOR_RDSR 0x05  /*!< Read Status Register instruction  */
#define sFLASH_CMD_NOR_MISR 0x35  /*!< Read Status Register instruction Midde Byte */
#define sFLASH_CMD_NOR_HISR 0x15  /*!< Read Status Register instruction Hige Byte */
#define sFLASH_CMD_NOR_RDID 0x9F  /*!< Read identification */
#define sFLASH_CMD_NOR_SE 0x20    /*!< Sector Erase instruction */
#define sFLASH_CMD_NOR_BE 0xD8    /*!< Block Erase instruction */
#define sFLASH_CMD_NOR_CE 0xC7    /*!< Chip Erase instruction */

#define sFLASH_WIP_FLAG 0x01 /*!< Write In Progress (WIP) flag */

#define FLASH_SECTOR_SIZE (4096)
typedef enum
{
    EXTERN_FLASH_STATE_INVALID, //无效的-未连接的
    // EXTERN_FLASH_STATE_OK,      //正常的
    EXTERN_FLASH_STATE_4M,
    EXTERN_FLASH_STATE_8M,
    EXTERN_FLASH_STATE_16M,
} extern_flash_state_t;

void flash_spi_init(void);
void sFLASH_NOR_Read(uint8_t *pBuffer, uint32_t ReadAddr, uint16_t NumByteToRead);
void sFLASH_NOR_Pageprogram(const uint8_t *pBuffer, uint32_t WriteAddr, uint16_t NumByteToWrite);
void sFLASH_NOR_SectorErase(uint32_t SectorAddr);
void sFLASH_NOR_ChipErase(void);

int flash_spi_write(uint32_t addr, uint8_t *p_data, int length);
extern_flash_state_t extern_flash_get_state(void);
void extern_flash_spi_io_uninit(void);

uint32_t sFLASH_NOR_ReadID(void);
#endif
